<template>
  <div class="goodsinfo-container">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <Slide :lunbotuList="newImgList" :idfull="false"></Slide>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">电影名：{{conList.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥88</del> &nbsp;&nbsp;预购价：
            <span class="newPrice">￥55</span>
          </p>
          <p>
            购买数量：
            <NumBox @getcount="getSelectedCount" :max="conList.year"></NumBox>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">{{conList.title}}--详情介绍</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>电影年份：{{conList.year}}</p>
          <p>剩余票数：60</p>
          <p>
            电影类型：
            <span v-for="item in conList.genres" :key="item.id">{{item + "、"}}</span>
          </p>
          <p>
            主要演员：
            <span v-for="item in newImgList" :key="item.id">{{item.name + "、"}}</span>
          </p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDet(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">电影评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
const API_PROXY = "https://bird.ioliu.cn/v1/?url=";
import Slide from "../slide/slide.vue";
import { Toast, Indicator } from "mint-ui";
import NumBox from "./numBox.vue";
export default {
  data() {
    return {
      newImgList: {},
      conList: [],
      id: this.$route.params.id,
      ballFlag: false,
      selectedCount: 1
    };
  },
  created() {
    this.getImg();
  },
  methods: {
    getImg() {
      Indicator.open({ text: "加载中...", spinnerType: "fading-circle" });
      this.$http
        .get(API_PROXY + "https://api.douban.com/v2/movie/subject/" + this.id)
        .then(result => {
          if (result.status === 200) {
            // 添加轮播图片
            result.body.casts.forEach(item => {
              item.src = item.avatars.small;
            });
            this.newImgList = result.body.casts;
            this.conList = result.body;
            Indicator.close();
            // console.log(result.body);
          } else {
            Toast("图片加载失败...");
            Indicator.close();
          }
        });
    },
    goDet(id) {
      // 跳转到图文详情
      this.$router.push({ name: "imgConDet", params: { id } });
    },
    goComment(id) {
      // 跳转到评论
      this.$router.push({ name: "imgConComment", params: { id } });
    },
    addToShopCar() {
      // 添加到购物车
      this.ballFlag = !this.ballFlag;
      var goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.conList.year,
        selected: true
      };

      this.$store.commit("addToCar", goodsinfo);
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;
      // 获取小球在页面的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // 获取徽章在页面的位置
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();
      // 计算小球移动的位置
      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px,${yDist}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count) {
      this.selectedCount = count;
      // console.log("父组件的值:" + count)
    }
  },
  components: {
    Slide,
    NumBox
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
  .newPrice {
    color: red;
    font-size: 15px;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 440px;
    left: 150px;
  }
}
</style>

